package com.gjm.demo.controller;

import com.gjm.demo.component.ComponentTest;
import com.gjm.demo.service.ServiceTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

@Controller//将对象交给spring管理
//@Controller("Controller")//改名之后获取对象就直接使用该名获取对象
public class ControllerTest {

    //属性注入
//    @Autowired
    private ComponentTest component;

    private ServiceTest service;
//
//    //构造方法注入
//    //指定使用某个构造方法注入
//    @Autowired
//    public ControllerTest(ComponentTest component) {
//        this.component = component;
//    }
//
//    @Autowired
//    public ControllerTest(ServiceTest service) {
//        this.service = service;
//    }
//
//    //只能存在一种构造方法,一参与二参不能同时存在
////    @Autowired
//    public ControllerTest(ComponentTest component, ServiceTest service) {
//        this.component = component;
//        this.service = service;
//    }

    //setter方法注入
    @Autowired
    public void setComponent(ComponentTest component) {
        this.component = component;
    }

    @Autowired
    public void setService(ServiceTest service) {
        this.service = service;
    }


    //默认使用无参构造方法注入
//    public ControllerTest() {
//
//    }

    public void print() {
        System.out.println("controller");
        System.out.println(component);
        System.out.println(service);
        component.print();
        service.print();
    }
}
